<?php
namespace app\backend\controller;

use app\backend\model\OrderModel;

class Order extends Base
{
    public function index()
    {
        $where = [];
        if(input('sn'))
            array_push($where, ['sn', '=', input('sn')]);

        if(input('mob'))
            array_push($where, ['delivery_phone', '=', input('mob') ]);

        if(input('store_name'))
            array_push($where, ['store_id', '=', \app\common\model\Store::where('name', input('store_name'))->value('id') ]);

        if(input('status'))
            array_push($where, ['status', '=', input('status') ]);

        $this->assign('data', [
            'title' => '店铺列表',
            'collection' => OrderModel::where($where)->order('id desc')->paginate(config('paginate.per_page')),
            'thead' => [
                '单号',
                '店铺',
                '姓名/手机号/地址',
                '总价格',
                '商品内容',
                '订单时间',
                '状态',
            ],
            'fields' => [
                'sn',function($row) {
                    return $row->store->name;
                },function($row) {
                    return $row['delivery_name'].'/'.$row['delivery_phone'].'/'.$row['delivery_address'];
                },'total_price','total_price','add_time','status',
            ],
            'buttons' => [
                ['title'=>'详情', 'onclick' => 'showBigWindow', 'url'=> url('details')], // 'onclick'=>'redirect',
            ],
            'searcher' => [
                ['type'=>'input','name'=>'sn','placeholder'=>'订单号'],
                ['type'=>'input','name'=>'mob','placeholder'=>'收货手机号'],
                ['type'=>'input','name'=>'store_name','placeholder'=>'店铺名'],
                ['type'=>'select','name'=>'status', 'option' => config('status.order')],
            ],
        ]);
        return $this->fetch('public/table_builder');
    }
    public function details()
    {
        $id = input('id');
        $order = \app\common\model\Order::find($id);
        $goods_info = [];
        foreach ($order->goods as $k => $g) {
            $goods = \app\common\model\Product::find($g->goods_id);
            $goods_info[' - 商品ID' . $g->goods_id] = '【 商品名称 】 ' . $goods->name .' 【 规格 】 ' . $g->goods_suk . ' 【 价格 】 ￥' . $g->price ;
        }
        $info = [
            "订单号" => $order->sn ,
            "店铺" => $order->store->name ,
            "状态" => $order->status ,
            "订单金额" => $order->total_price ,
            "收货人" => $order->delivery_name ,
            "配送电话" => $order->delivery_phone ,
            "配送地址" => $order->delivery_address ,
            "配送时间" => $order->delivery_time ,
            "订单时间" => $order->add_time ,
            "订单备注" => $order->remark ,
            "退款状态" => $order->refund_status ,
            "退款说明" => $order->refund_reason_explain ,
            "商品信息" => '',
        ];
        ;
        return $this->fetch('public/details_builder', ['data' => [
            'title' => '订单详情',
            'info' => array_merge($info , $goods_info),
        ]]);
    }
    public function create()
    {
        if(request()->isPost()) {
            $data = input('post.');
            $data['expire_time'] = date('Y/m/d 0:0', strtotime('today'));
            return $this->modelCreate('app\backend\model\MerchantModel', $data, 'merchant_create', '开户');
//            if($id) {
//                return $this->modelUpdate('app\backend\model\ExtensionModel', $data, ['id' => $id]);
//            }else{
//            }
        }else {
            return $this->fetchFormPageHtml('app\backend\model\MerchantModel', __FUNCTION__);
        }
    }


}
